package middle;

public class Solution_12 {

    public String intToRoman(int num) {
        char[][] signs = {{'I', 'V'}, {'X', 'L'}, {'C', 'D'}, {'M'}};
        StringBuffer buffer = new StringBuffer();
        int bit = 0;
        while (num > 0) {
            int mod = num % 10;
            if (mod == 9) {
                buffer.append(signs[bit + 1][0]);
                buffer.append(signs[bit][0]);
            } else if (mod >= 4) {
                for (int i = 6; i <= mod; i++) {
                    buffer.append(signs[bit][0]);
                }
                buffer.append(signs[bit][1]);
                if (mod == 4) {
                    buffer.append(signs[bit][0]);
                }
            } else {
                for (int i = 1; i <= mod; i++) {
                    buffer.append(signs[bit][0]);
                }
            }
            num /= 10;
            bit++;
        }
        return buffer.reverse().toString();
    }

    public static void main(String[] args) {
        Solution_12 model = new Solution_12();
        System.out.println(model.intToRoman(3));
        System.out.println(model.intToRoman(4));
        System.out.println(model.intToRoman(9));
        System.out.println(model.intToRoman(58));
        System.out.println(model.intToRoman(1994));
    }
}
